import { type } from "os";

// 封装存储类
export class MyStorage {
  storage: Storage;
  constructor(storage: Storage = localStorage) {
    this.storage = storage;
  }
  setItem(key: string, value: any) {
    if (/* 是不是字符串 */ typeof value === "string") {
      this.storage.setItem(key, value);
    } else if (
      typeof value === "bigint" ||
      typeof value === "boolean" ||
      typeof value === "number"
    ) {
      this.storage.setItem(key, value.toString());
    } else if (typeof value === "object" && value !== null) {
      this.storage.setItem(key, JSON.stringify(value));
    }
  }
  getItem(key: string) {
    const value = this.storage.getItem(key);
    try {
      if(value) return JSON.parse(value);
    } catch {
      return value;
    }
  }
}

const l = new MyStorage();

l.setItem("arr", [1, 2, 3, 4, { name: "张三" }]);
l.setItem("str", "1231231231");
l.setItem("bool", true);

const result1 = l.getItem("arr");
const result2 = l.getItem("str");
const result3 = l.getItem("bool");

console.log(result1, result2, result3);
